package com.example.demo3.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.DictData;
import com.example.demo3.entity.DictType;
import com.example.demo3.mapper.DictDataMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class DictDataService extends ServiceImpl<DictDataMapper, DictData> {
    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private DictDataMapper dictDataMapper;

    public List<DictData> getDictDataByType(String type){
        QueryWrapper<DictData> queryWrapper=new QueryWrapper<DictData>()
                .select("ID","TYPE","DICT_VALUE","DICT_TEXT","PARENT_ID")
                .eq("DEL_FLAG",0)
                .eq("TYPE",type)
                .orderByAsc("ID");
        List<DictData> list = dictDataMapper.selectList(queryWrapper);
        return list;
    }

    public DictData getDictDataById(String id){
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<DictData>()
                .eq("ID",id);
        DictData dictData = dictDataMapper.selectOne(queryWrapper);
        return dictData;
    }

    public Page<DictData> getSelectPage(String type, String dictText, String id, int pageindex, int pagesize){
        Page<DictData> page=new Page<DictData>().setCurrent(pageindex).setSize(pagesize);
        QueryWrapper<DictData> queryWrapper=new QueryWrapper<DictData>()
                .like(StringUtils.isNotBlank(type),"TYPE",type)
                .like(StringUtils.isNotBlank(dictText),"DICT_TEXT",dictText)
                .eq(StringUtils.isNotBlank(id),"PARENT_ID",id)
                .eq("DEL_FLAG",0)
                .orderByAsc("DICT_VALUE");
        dictDataMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Transactional
    public ResponseMessage insertDictData(DictData dictData){
        dictData.setDelFlag(0);
        dictData.setCreateUser("wcm");
        dictData.setCreateTime(new Date());
        dictData.setCompanyCode("1");
        DictType dictType = dictTypeService.getDciTypeById(dictData.getParentId());
        String typeValue = "";
        if(null != dictType){
            //字段类型不为空 说明新增的是字段类型下的数据
            dictData.setParentId(null);
            dictData.setType(dictType.getDictType());
            typeValue = dictType.getDictType();
        }else{
            //字典类型为空  去字典数据表里查
            DictData dictData1 = getDictDataById(dictData.getParentId());
            dictData.setType(dictData1.getType());
            typeValue = dictData1.getType();
        }
        String msg = "";
        List<DictData> checkDictValue = list(Wrappers.<DictData>lambdaQuery().eq(DictData::getCompanyCode,dictData.getCompanyCode())
                .eq(DictData::getDictValue,dictData.getDictValue()).eq(DictData::getDelFlag,0)
                .eq(DictData::getType,typeValue));
        if(CollectionUtils.isNotEmpty(checkDictValue)){
            msg = "字典编号已重复！";
            return ResponseMessage.error(msg);
        }

        List<DictData> checkDictText = list(Wrappers.<DictData>lambdaQuery().eq(DictData::getCompanyCode,dictData.getCompanyCode())
                .eq(DictData::getDictText,dictData.getDictText()).eq(DictData::getDelFlag,0)
                .eq(DictData::getType,typeValue));
        if(CollectionUtils.isNotEmpty(checkDictText)){
            msg = "字典名称已重复！";
            return ResponseMessage.error(msg);
        }
        save(dictData);
        msg = "新增"+dictData.getDictText()+"成功!";
        return ResponseMessage.ok(null,msg);
    }

    @Transactional
    public ResponseMessage deleteDcitData(String id){
        DictData dictData = getById(id);
        dictData.setDelFlag(1);
        updateById(dictData);
        return ResponseMessage.ok("删除成功");
    }

    @Transactional
    public ResponseMessage delBatch(String ids){
        String[] arr = StringUtils.split(ids, ",");
        List<String> idsList = Arrays.asList(arr);
        /*dictDataMapper.deleteBatchIds(idsList);*/
        List<DictData> dictDataList = dictDataMapper.selectBatchIds(idsList);
        for (DictData dictData:dictDataList) {
            dictData.setDelFlag(1);
        }
        updateBatchById(dictDataList,1000);
        return ResponseMessage.ok("删除成功");
    }



}
